var express=require('express');
var utility=require('utility');
var cheerio=require('cheerio');
var superagent=require('superagent');
var eventproxy=require('eventproxy');
var url=require('url');
var async=require('async');

var app=express();
var baseUrl='https://cnodejs.org/'

app.get('/',function(req,res,next){
    superagent.get(baseUrl).end(function(err,sres){
        if(err){
            console.log('爬去信息失败');
            console.log(err);
            next(err)
        };
        console.log('get info');
        var $=cheerio.load(sres.text);
        
        var item=[];
        var detail_href=[];
        var sresText=sres.text;

        $('#topic_list .topic_title').each(function(index,element){
            var $element=$(element);
            var tempRef=url.resolve(baseUrl,$element.attr('href'));
            // console.log(tempRef);
            item.push({
                title:$element.attr('title'),
                href:tempRef,
                comment:''
            });
            detail_href.push(tempRef);
        });
        var ep=new eventproxy();
        ep.after('comment',40,function(comments){
            console.log('comment finished');
            comments.forEach(function(comment,index){
                item[index].comment=comment;
            });

            res.send(item);
        })

        // res.send(sresText);
        // detail_href.forEach(function(d_href){
        //     superagent.get(d_href).end(function(err,tempRes){
        //         if(err){console.log(err)};
        //         var temp$=cheerio.load(tempRes.text);
        //         var temp_comment=temp$('.reply_content p').eq(0).text().trim();
        //         ep.emit('comment',temp_comment);

        //     })
        // })
        async.mapLimit(detail_href,40,function(d_href,callback){
            superagent.get(d_href).then(function(tempRes){
                if(err){console.log('err |:',err)};
                var temp$=cheerio.load(tempRes.text);
                var temp_comment=temp$('.reply_content p').eq(0).text().trim();
                console.log(temp_comment);
                ep.emit('comment',temp_comment);
            })
        })

    })

});

app.listen(3000,function(){
    console.log('heihei')
})